<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('order_items', function (Blueprint $table) {
            $table->id();
            $table->foreignId('order_id')->constrained()->onDelete('cascade')->comment('订单ID');
            $table->foreignId('product_id')->constrained()->onDelete('cascade')->comment('产品ID');
            $table->foreignId('variant_id')->nullable()->constrained('product_variants')->onDelete('cascade')->comment('变体ID');
            $table->string('sku')->comment('SKU');
            $table->string('product_name')->comment('产品名称');
            $table->string('variant_name')->nullable()->comment('变体名称');
            $table->json('specifications')->nullable()->comment('规格信息');
            $table->decimal('price', 10, 2)->comment('单价');
            $table->integer('quantity')->comment('数量');
            $table->decimal('total_amount', 10, 2)->comment('小计金额');
            $table->decimal('discount_amount', 10, 2)->default(0)->comment('折扣金额');
            $table->decimal('tax_amount', 10, 2)->default(0)->comment('税费金额');
            $table->text('notes')->nullable()->comment('备注');
            $table->enum('status', ['confirmed', 'processing', 'shipped', 'cancelled', 'refunded'])
                  ->default('confirmed')
                  ->comment('订单项状态：confirmed-已确认，processing-处理中，shipped-已发货，cancelled-已取消，refunded-已退款');
            $table->timestamps();
            
            $table->index(['order_id', 'product_id']);
            $table->index('sku');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('order_items');
    }
};
